package com.supostacks.xuetanginterfaces.facade;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.supostacks.application.application.IResourceInfoApplication;
import com.supostacks.application.server.es.EsResourceInfoRepository;
import com.supostacks.domain.vo.BaseVo;
import com.supostacks.domain.vo.ResourceInfoVo;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class EsResourceInfoApplication {

    @Resource
    private IResourceInfoApplication resourceInfoApplication;
    @Resource
    EsResourceInfoRepository esResourceInfoRepository;

    @PostConstruct
    public void es_init() {
        boolean exists = esResourceInfoRepository.exists();
        if (!exists) {
            esResourceInfoRepository.createIdx();
        }

        String option = "{'is_list':['eq',1]}";
        JSONObject options = JSONObject.parseObject(option);
        List<BaseVo> baseVos = resourceInfoApplication.selectList(options);
        for (BaseVo baseVo : baseVos) {
            BaseVo baseVo1 = esResourceInfoRepository.selectById(baseVo.getId());
            if (baseVo1 == null) {
                esResourceInfoRepository.save((ResourceInfoVo) baseVo);
            }
        }
    }

}
